//
//  SettingViewController.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/17.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  设置页面

import UIKit

protocol SettingViewControllerDelegate {
    func back()
}

class SettingViewController: UITableViewController,SettingCellDelegate {

    var delegate : SettingViewControllerDelegate?
    
    //验证是够第三方授权是否绑定手机,并且是否设置密码
    
    
    //表格数据源
    lazy var dataArr : NSMutableArray = {
        var data = NSMutableArray()
        data = ["编辑个人资料","密码修改","绑定手机号","收货地址"]

        return data
    }()
    
    //图片
    lazy var imgName : NSMutableArray = {
        var data = NSMutableArray()
        data = ["perInfo","password","phone","address"]
        
        return data
    }()
    
    //导航栏设置还原
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = false
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
        
        
        if ((localSave.object(forKey: personInfo) as? NSDictionary) != nil) {
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //标题
        self.navigationItem.title = "设置"

        
        //设置表格样式
        setStyle()
    }

}

// MARK:- 表格代理和方法
extension SettingViewController {
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        switch section {
        case 0:
            return dataArr.count
        default:
            return 1
        }
    }
    
    //设置section 间距
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        return 1
    }
    
    
    //设置数据源
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as? SettingCell
        if cell == nil {
            cell = SettingCell(style: .default, reuseIdentifier: "cellID")
            tableView.separatorStyle = UITableViewCellSeparatorStyle.none
            //监听代理
            cell?.delegate = self
            cell?.imgView.image = UIImage.init(named: imgName[indexPath.row] as! String)
        }

        switch indexPath.section {
            //第一组时，显示
        case 0:
            cell?.btn.isHidden = true
//            cell?.imgView.image = UIImage.init(named: "ico")
            cell?.nameLabel.text = dataArr[indexPath.row] as? String
            //第二组时，不显示
        default:
            cell?.backgroundColor = UIColor.clear
            cell?.line.isHidden = true
            cell?.imgView.isHidden = true
            cell?.nameLabel.isHidden = true
            cell?.disclosureImg.isHidden = true
            break
        }

        return (cell)!
   
    }
    
    
    
    //设置行高
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 45
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("点击了 ",indexPath.row)
        switch indexPath.row {
            //编辑个人资料
        case 0:
            
            self.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(PersonInfoViewController(), animated: true)
//            self.hidesBottomBarWhenPushed = false

            break
            //密码修改
        case 1:
            self.navigationController?.pushViewController(ChagePassViewController(), animated: true)
            break
            //绑定手机号
        case 2:
            //检测单例中是否有值，已绑定手机号码，若单例没有，从本地取判断
            //1.PersonInfoModel.shared.bindPhone = list["password"] as? String（值为false）,可以单击跳转到绑定手机号码界面,若已绑定，提示已绑定
            if PersonInfoModel.shared.bindPhone != "" {
                if PersonInfoModel.shared.bindPhone == "false" {
                    self.navigationController?.pushViewController(BindPhoneNumViewController(), animated: true)
                } else {
                    
                    guard let personData = localSave.object(forKey: personInfo) as? NSDictionary else {
                        return
                    }
                    
                    guard var telNum = personData["phone"] as? NSString else {
                        return
                    }
                    
                    
                    //判断手机号是否为空
                    if telNum == "" {
                        
                        self.navigationController?.pushViewController(BindPhoneNumViewController(), animated: true)
                        
                    } else {
                        //替换指定字符为****
                        let range = NSMakeRange(4, 4)
                        
                        telNum = telNum.replacingCharacters(in: range, with: "****") as NSString
                        
                        let alertView = UIAlertController.init(title: "提示", message: "已绑定手机号:\(telNum)", preferredStyle: .alert)
                        alertView.addAction(UIAlertAction.init(title: "确定", style: .default, handler: nil))
                        
                        self.navigationController?.present(alertView, animated: true, completion: nil)
                    }

                }
            } else if localSave.object(forKey: thirdHavePass) as? String == "false" {
                    self.navigationController?.pushViewController(BindPhoneNumViewController(), animated: true)
            }
            
            
            break
            
        //收货地址
        case 3:
//            self.navigationController?.pushViewController(GetGoodsViewController(), animated: true)
            self.navigationController?.pushViewController(GetGoodsVieController(), animated: true)
            break

        default:
            break
        }
    }

}



// MARK:- 设置表格样式
extension SettingViewController {
   fileprivate func setStyle() -> Void {
    
        tableView = UITableView(frame: self.view.bounds, style: .grouped)
    }
}


// MARK:- 监听代理函数
extension SettingViewController {
    func showLogVC() {
        print("退出出来")
        
        CustomAlertView.shared.alertWithTitle(strTitle: logoutSuccess)

        //延迟一秒执行，避免退出的dismissViewController 造成主线程阻塞
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0) {
            let mainVC = MainViewController()
            
            mainVC.mvc = MyVC()
            
            UIApplication.shared.keyWindow?.rootViewController = mainVC
        }

    }
}




